<?php

namespace app\commands;

use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\product\ProductAreaList;
use app\models\product\ProductCategory;
use app\models\SetProductCommission;
use app\models\user\User;
use app\models\user\UserLevel;
use yii\console\Controller;

class ProductController extends Controller
{
    // 等级参数
    protected $list = [
        ['title' => 'VIP', 'price' => 999, 'is_repeat' => true],
        ['title' => '市场总监', 'price' => 3999],
        ['title' => '董事', 'price' => 0, 'count' => 2],
        ['title' => '品牌合伙人', 'price' => 39800, 'count' => 100],
        ['title' => '联合创始人', 'price' =>98000, 'count' => 300],
    ];

    /**
     * 改模式使用
     */
    public function actionIndex()
    {

        try {
            // 下架分类
            $this->removeProduct();
            echo '产品下架到成功', "\n";

            // 下架产品
            $this->removeCategory();
            echo '分类下架到成功', "\n";

            // 添加分类  添加产品
            $this->addProduct();
            echo '测试产品添加成功', "\n";

            // 停止之前的等级
            $this->level();
            echo '等级处理完成', "\n";

            // 取消会员等级
            $this->userLevel();
            echo '停止会员等级处理完成', "\n";

        } catch (\Exception $e) {
            var_dump($e->getLine());
            var_dump($e->getMessage());
        }
    }

    public function removeProduct()
    {
        Product::updateAll(['status' => 0], ['status' => 1]);
    }

    public function removeCategory()
    {
        ProductCategory::updateAll(['status' => 0], ['status' => 1]);
    }

    public function addProduct()
    {
        // 关闭两个区
        ProductArea::updateAll(['status' => 0], ['>=', 'id', 8]);

        foreach ($this->list as $item) {

            if ($item['price'] == 0) {
                continue;
            }

            // 分类
            $category = new ProductCategory();
            $category->name = $item['title'];
            // 随便找一个图片
            $category->image_id = 19694;
            // $category->image_id = 19764;
            $category->save();

            // 产品
            $product = new Product();
            $product->title = $item['title'] . '套装';
            $product->price = $product->price_old = $item['price'];
            $product->category_id = $category->id;
            $product->save();

            // 复购差价
            $diff = new SetProductCommission();
            $diff->product_id = $product->id;
            $diff->new_vip1 = 999;
            $diff->new_vip2 = 699;
            $diff->new_vip3 = 599;
            $diff->new_vip4 = 519;
            $diff->new_vip5 = 489;
            $diff->save();

            // 区域
            $area = new ProductAreaList();
            $area->product_id = $product->id;
            $area->area_id = Product::$areaMap['REPORT'];
            $area->save();

            // 复购
            if (isset($item['is_repeat']) && $item['is_repeat']) {
                // 产品
                $product = new Product();
                $product->title = '复购套装';
                $product->price = $product->price_old = $item['price'];
                $product->category_id = $category->id;
                $product->save();

                // 复购差价
                $diff = new SetProductCommission();
                $diff->product_id = $product->id;

                $diff->new_vip1 = 999;
                $diff->new_vip2 = 699;
                $diff->new_vip3 = 599;
                $diff->new_vip4 = 519;
                $diff->new_vip5 = 489;

                $diff->save();

                // 区域
                $area = new ProductAreaList();
                $area->product_id = $product->id;
                $area->area_id = Product::$areaMap['REPEAT'];
                $area->save();
            }
        }
    }

    public function level()
    {
        UserLevel::updateAll(['hide' => 1, 'value' => 1], ['>', 'value', 1]);

        foreach ($this->list as $key => $item) {
            $level = new UserLevel();
            $level->name = $level->display = $item['title'];
            $level->key = 'vip' . ($key + 1);
            $level->value = 101 + $key;
            $level->save();
        }
    }

    public function userLevel()
    {
        User::updateAll(['level' => 0]);
    }
}